/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.LoaiKhachHangControl;
import edu.nii.qlks.DataTransferObject.LoaiKhachHangInfo;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jfrm_CapNhatLoaiKhachHang extends javax.swing.JInternalFrame {

    /**
     * Creates new form jfrm_CapNhatLoaiKhachHang
     */
    LoaiKhachHangControl loaikhCT;
    LoaiKhachHangInfo loaikhInfo;

    public jfrm_CapNhatLoaiKhachHang() {
        initComponents();
        loaikhCT = new LoaiKhachHangControl();
        loaikhInfo = new LoaiKhachHangInfo();
        
        Load_TenLoaiKhach();
    }
    
    public void Load_TenLoaiKhach(){
        try {
            Vector vt = loaikhCT.LayDS();
            if (vt.size() > 0){
                for (int i = 0; i < vt.size(); i++){
                    Vector vt_row = (Vector)vt.get(i);
                    String tenloaikh = vt_row.get(1).toString().trim();
                    jComboBox_TenLoaiKhachhang.addItem(tenloaikh);
                }
            }
            else{/////
            }
        } catch (Exception ex) {
            Logger.getLogger(jfrm_CapNhatDonGiaLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jXLabel3 = new org.jdesktop.swingx.JXLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jTextField_HeSoLoaiKhachHang = new javax.swing.JTextField();
        jComboBox_TenLoaiKhachhang = new javax.swing.JComboBox();
        jButton_CapNhat = new javax.swing.JButton();
        jButton_Thoat = new javax.swing.JButton();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jXLabel3.setText("CẬP NHẬT LOẠI KHÁCH HÀNG");
        jXLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jXPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin loại khách hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jXLabel1.setText("Tên Loại Khách Hàng");
        jXLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel2.setText("Hệ Số Loại Khách Hàng");
        jXLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTextField_HeSoLoaiKhachHang.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTextField_HeSoLoaiKhachHang.setToolTipText("Nhập hệ số loại khách hàng");

        jComboBox_TenLoaiKhachhang.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComboBox_TenLoaiKhachhang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_TenLoaiKhachhangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField_HeSoLoaiKhachHang)
                    .addComponent(jComboBox_TenLoaiKhachhang, 0, 202, Short.MAX_VALUE))
                .addContainerGap(147, Short.MAX_VALUE))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel1Layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_TenLoaiKhachhang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_HeSoLoaiKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59))
        );

        jButton_CapNhat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_CapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton_CapNhat.setText("Cập nhật");
        jButton_CapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CapNhatActionPerformed(evt);
            }
        });

        jButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_16.png"))); // NOI18N
        jButton_Thoat.setText("Đóng");
        jButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton_CapNhat)
                .addGap(29, 29, 29)
                .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(144, 144, 144))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 57, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(265, 265, 265)
                .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jXLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59)
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_CapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(75, 75, 75))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox_TenLoaiKhachhangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_TenLoaiKhachhangActionPerformed
        if (jComboBox_TenLoaiKhachhang.getItemCount() > 0){
            try {
                String str = jComboBox_TenLoaiKhachhang.getSelectedItem().toString().trim();
                Vector vt = loaikhCT.LayDS("TenLoaiKhach = N'" + str + "'");
                Vector vt_row = (Vector)vt.get(0);
                jTextField_HeSoLoaiKhachHang.setText(vt_row.get(2).toString().trim());
            } catch (Exception ex) {
                //Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jComboBox_TenLoaiKhachhangActionPerformed

    private void jButton_CapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CapNhatActionPerformed
        // TODO add your handling code here:
        if (jTextField_HeSoLoaiKhachHang.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Hệ số loại phòng không được để trống!", "Lỗi", JOptionPane.ERROR_MESSAGE);
        } else {


            try {
                loaikhInfo.setHeSoLoaiKhach(Float.parseFloat(jTextField_HeSoLoaiKhachHang.getText()));
                loaikhInfo.setTenLoaiKhach(jComboBox_TenLoaiKhachhang.getSelectedItem().toString());
                //////////////////////////////////////////////////////////////////////////////////
                 
                try {
                    Vector vt = loaikhCT.LayDS("TenLoaiKhach = N'" + loaikhInfo.getTenLoaiKhach() + "'");
                    Vector vt_row = (Vector)vt.get(0);
                    loaikhInfo.setMaLoaiKhach(vt_row.get(0).toString().trim());
                    
                    loaikhCT.Update(loaikhInfo);
                    
                    JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    Logger.getLogger(jfrm_CapNhatLoaiKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Cập nhật thất bại!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Hệ số loại khách hàng chỉ được nhập bằng số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                jTextField_HeSoLoaiKhachHang.requestFocus();
                jTextField_HeSoLoaiKhachHang.setText("");
                return;
            }



        }
    }//GEN-LAST:event_jButton_CapNhatActionPerformed

    private void jButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton_ThoatActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_CapNhat;
    private javax.swing.JButton jButton_Thoat;
    private javax.swing.JComboBox jComboBox_TenLoaiKhachhang;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField_HeSoLoaiKhachHang;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXLabel jXLabel3;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
